package com.example.warehouse.sys.controller;

import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.security.CurrentPrincipal;
import com.example.warehouse.common.web.JsonResult;
import com.example.warehouse.sys.pojo.entity.SysNotice;
import com.example.warehouse.sys.pojo.param.AddNewNoticeParam;
import com.example.warehouse.sys.pojo.vo.DeptStandardVO;
import com.example.warehouse.sys.pojo.vo.NoticeListItemVO;
import com.example.warehouse.sys.pojo.vo.NoticeSelectVO;
import com.example.warehouse.sys.pojo.vo.NoticeStandardVO;
import com.example.warehouse.sys.service.INoticeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author 欧胤
 * @version 1.0
 * @description: TODO
 * @date 2023/7/6 19:30
 */
@Slf4j
@RestController
@RequestMapping("/sys/notice")
@Api(tags = "4.1. 通知管理")
@Validated
public class NoticeController {
    @Autowired
    private INoticeService service;

    public NoticeController() {
        log.info("创建控制器对象:NoticeController");
    }

    @ApiOperation("发布通知")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/sys/notice/insert/addNotice')")
    public JsonResult insert(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal, @Validated AddNewNoticeParam addNewNoticeParam) {
        log.debug("开始处理 [用户注册] 的请求, 参数: {} ", addNewNoticeParam);

        service.addNew(currentPrincipal,addNewNoticeParam);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/sys/notice/delete')")
    @ApiOperation("根据ID删除通知")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的文章ID值！") Long id) {
        log.debug("开始处理【根据ID删除通知】的请求，参数：{}", id);
        service.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询通知详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通知ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的通知ID值！") Long id) {
        log.debug("开始处理【根据ID查询通知详情】的请求，参数：{}", id);
        NoticeStandardVO queryResult = service.getStandardById(id);
        return JsonResult.ok(queryResult);
    }
    @GetMapping("")
   @PreAuthorize("hasAuthority('/sys/notice/read')")
    @ApiOperation("查询通知列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
    })
    public JsonResult list(@Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【查询通知列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<NoticeListItemVO> pageData = service.list(pageNum);
        return JsonResult.ok(pageData);
    }
    @PostMapping("/select")
    @ApiOperation("根据三种查询通知详情")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({})
    public JsonResult getStandardByNotice(
            @Validated NoticeSelectVO notice,Integer page) {
        log.debug("开始处理【根据ID查询通知详情】的请求，参数：{}", notice);
        Integer pageNum = page == null ? 1 : page;
        PageData<NoticeListItemVO> pageData = service.list(notice,pageNum);
        return JsonResult.ok(pageData);
    }
}
